package com.example.minzhengtong.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.example.minzhengtong.Const
import com.example.minzhengtong.HttpRequest
import com.example.minzhengtong.base.BaseA
import com.example.minzhengtong.data.UserBean
import com.example.minzhengtong.databinding.ActivityGjjAccountInfoBinding
import com.example.minzhengtong.utils.SpUtil
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * created by WY on 2021/5/31.
 * 账户信息
 */
class GjjAccountInfoA : BaseA() {

    companion object{
        fun enterThis(context: Context){
            val intent = Intent(context,GjjAccountInfoA::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var binding : ActivityGjjAccountInfoBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        overridePendingTransition(0,0)
        binding = ActivityGjjAccountInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setToolBarVisiabale(false)

        ImmersionBar.with(this).statusBarDarkFont(true).titleBar(binding.titleBar).init()

        setData()

    }

    fun setData(){
        if (SpUtil.getInstance().existKey(Const.USER_BEAN)){
            val bean = SpUtil.getInstance().getObject(Const.USER_BEAN) as UserBean?
            bean?.data?.apply {
                //个人账户
                binding.grzh.text = if (uzh.length>=5){
                    var starText = ""
                    for (i in 0 .. (uzh.length-6)){
                        starText += "*"
                    }
                    "${uzh.subSequence(0,2)}${starText}${uzh.subSequence(uzh.length-3,uzh.length)}"
                } else {
                    uzh
                }
                //证件号码
                binding.zjhm.text = if (zjhm.length > 5){
                    var starText = ""
                    for (i in 0 ..(zjhm.length-6)){
                        starText += "*"
                    }
                    "${zjhm.subSequence(0,2)}${starText}${zjhm.subSequence(zjhm.length-3,zjhm.length)}"
                }else{
                    zjhm
                }
                //婚姻状况
                //手机号码
                binding.sjhm.text = phone
                //签约银行
                binding.qyyh.text = bank_name
                //银行卡号
                binding.yhkh.text = if (card_number.length > 5){
                    var starText = ""
                    for (i in 0 ..(card_number.length-6)){
                        starText += "*"
                    }
                    "${card_number.subSequence(0,2)}${starText}${card_number.subSequence(card_number.length-3,card_number.length)}"
                }else{
                    card_number
                }
                //账户类型
                binding.zhlx.text = blxy
                //账户状态
                binding.zhzt.text = personal_account_status
                //个人缴存基数
                binding.grjcjs.text = jcjs
                //个人缴存比例
                binding.grjcbl.text = grbl
                //个人缴存金额
                binding.grjcje.text = gryh
                //单位缴存比例
                binding.dwjcbl.text = dwbl
                //单位缴存金额
                binding.dwjcje.text = dwyh
                //单位名称
                binding.dwmc.text = company
                //单位账号
                binding.dwzh.text = if (dwzh.length > 5){
                    var starText = ""
                    for (i in 0 ..(dwzh.length-6)){
                        starText += "*"
                    }
                    "${dwzh.subSequence(0,2)}${starText}${dwzh.subSequence(dwzh.length-3,dwzh.length)}"
                }else{
                    dwzh
                }
            }
        }else{
            HttpRequest().detail(baseContext)
        }
    }

    override fun requestSuccess(json: String) {
        super.requestSuccess(json)
        val bean = Gson().fromJson(json, UserBean::class.java)
        bean?.data?.apply {
            //个人账户
            binding.grzh.text = if (uzh.length>=5){
                var starText = ""
                for (i in 0 .. (uzh.length-6)){
                    starText += "*"
                }
                "${uzh.subSequence(0,2)}${starText}${uzh.subSequence(uzh.length-3,uzh.length)}"
            } else {
                uzh
            }
            //证件号码
            binding.zjhm.text = if (zjhm.length > 5){
                var starText = ""
                for (i in 0 ..(zjhm.length-6)){
                    starText += "*"
                }
                "${zjhm.subSequence(0,2)}${starText}${zjhm.subSequence(zjhm.length-3,zjhm.length)}"
            }else{
                zjhm
            }
            //婚姻状况
            //手机号码
            binding.sjhm.text = phone
            //签约银行
            binding.qyyh.text = bank_name
            //银行卡号
            binding.yhkh.text = card_number
            //账户类型
            binding.zhlx.text = blxy
            //账户状态
            binding.zhzt.text = personal_account_status
            //个人缴存基数
            binding.grjcjs.text = y_money
            //个人缴存比例
            binding.grjcbl.text = grbl
            //个人缴存金额
            binding.grjcje.text = gryh
            //单位缴存比例
            binding.dwjcbl.text = dwbl
            //单位缴存金额
            binding.dwjcje.text = dwyh
            //单位名称
            binding.dwmc.text = company
            //单位账号
            binding.dwzh.text = dwzh
        }
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0,0)
    }

}